<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :lg="8" :md="8" :sm="12" :xl="8" :xs="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          添加
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :lg="16" :md="16" :sm="12" :xl="16" :xs="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.name"
              clearable
              placeholder="请输入资源名称"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :row-key="'id'"
      :tree-props="{ children: 'childList' }"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        label="资源名称"
        prop="name"
        width="200"
      />
      <el-table-column align="center" label="路径" prop="url" />
      <el-table-column align="center" label="图标" prop="icon" />
      <el-table-column align="center" label="类型" prop="type">
        <template #default="{ row }">
          <el-tag v-if="row['type'] === 0" size="medium">菜单</el-tag>
          <el-tag v-if="row['type'] === 1" size="medium">子菜单</el-tag>
          <el-tag v-if="row['type'] === 2" size="medium">按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="排序" prop="sort" />
      <el-table-column align="center" label="权限" prop="permission" />
      <el-table-column align="center" label="是否启用" prop="isEnable">
        <template #default="{ row }">
          <el-switch
            v-model="row.isEnable"
            :active-value="1"
            :disabled="isChanging"
            :inactive-value="0"
            @change="changeStatus(row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="85"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <resource-edit ref="edit" :tree-data="list" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { doDelete, getList, updateResource } from '@/api/resource'
  import ResourceEdit from './components/ResourceEdit'

  export default {
    name: 'MenuManagement',
    components: {
      ResourceEdit,
    },
    data() {
      return {
        list: [],
        listLoading: true,
        isChanging: false,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          name: '',
          pageNum: 1,
          pageSize: 10,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      changeStatus(row) {
        if (row.id === undefined || row.id === '' || row.id === null) {
          return
        }
        const newState = row.isEnable
        const initState = row.isEnable === 1 ? 0 : 1
        this.isChanging = true
        updateResource({ id: row.id, isEnable: newState })
          .then((res) => {
            if (res && res.status === 0) {
              this.$baseMessage(
                '状态修改成功！',
                'success',
                'vab-hey-message-success'
              )
            } else {
              this.$baseMessage(
                res ? res.msg : '修改失败！',
                'error',
                'vab-hey-message-error'
              )
              row['isEnable'] = initState
            }
          })
          .catch((e) => {
            console.log(e)
            row['isEnable'] = initState
            this.$baseMessage(
              e ? e.message : '操作失败！',
              'error',
              'vab-hey-message-error'
            )
          })
          .then(() => {
            this.isChanging = false
          })
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            try {
              const res = await doDelete(row.id)
              if (res && res.status === 0) {
                this.$baseMessage(
                  '删除成功！',
                  'success',
                  'vab-hey-message-success'
                )
                await this.fetchData()
              } else {
                this.$baseMessage(
                  res ? res.msg : '删除失败！',
                  'error',
                  'vab-hey-message-error'
                )
              }
            } catch (e) {
              console.log(e)
              this.$baseMessage(
                e ? e.message : '删除失败！',
                'error',
                'vab-hey-message-error'
              )
            }
          })
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        try {
          const res = await getList(this.queryForm.name)
          if (res && Array.isArray(res.data)) {
            this.list = res.data
          }
        } catch (e) {
          console.log(e)
        }
        this.listLoading = false
      },
    },
  }
</script>
